package com.cuit.qx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.qx.bean.*;
import com.cuit.qx.dao.WarningMapper;
import com.cuit.qx.job.TimingProcessorJob;
import com.cuit.qx.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * @Author:wjd
 * @Description: 实况处理
 * @date:Created in 16:37 2020/10/27
 * @Modified By:
 */
@Service
public class WarningDataHandleImpl extends ServiceImpl<WarningMapper, Warning> implements WarningDataHandle {

    private static final Logger LOG = LoggerFactory.getLogger(WarningDataHandleImpl.class);

    @Autowired
    WarningService warningService;
    @Autowired
    ThresholdManagementService thresholdManagementService;
    @Autowired
    InterfaceConfigService interfaceConfigService;
    @Autowired
    private TemScaleService temScaleService;

    @Resource
    private WindScaleService windScaleService;

    @Resource
    private WeatherStationService weatherStationService;

    @Resource
    private WarningWindService warningWindService;

    /**
     * @return
     * @Author:wjd on 2020/10/27 16:39
     * @param:
     * @Description: 实况处理入库
     */
    @Override
    public void dataHandle(String identifler, String data, String number) {
        warningHandle(identifler, data, number);
    }


    /**
     * @return
     * @Author:wjd on 2020/10/27 18:03
     * @param: tem：温度、 hum:湿度、 rain:降水、 wind 风速、 rain 降水
     * @Description:
     */
    public void warningHandle(String identifler, String data, String number) {
        BigDecimal bigDecimal = new BigDecimal(data);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String presentDate = format.format(new Date());
        List<Threshold> list = thresholdManagementService.list();
        switch (identifler) {
            case "hum":
                list.stream().forEach(item -> {
                    if (number.equals(item.getStation())) {
                        if (bigDecimal.compareTo(new BigDecimal(item.getHumidity())) == 1 &&
                                bigDecimal.compareTo(new BigDecimal(item.getHumidityMax())) == -1) {
                            Warning warning = new Warning();
                            warning.setCreateTime(presentDate);
                            warning.setStationName(item.getStationName());
                            warning.setStationNumber(item.getStation());
                            warning.setStationSite(item.getAddress());
                            WeatherStation site = weatherStationService.getStationInfoByName(item.getStation());
                            warning.setLat(String.valueOf(site.getLatitude()));
                            warning.setLon(String.valueOf(site.getLongitude()));
                            warning.setStationCounty(site.getStationCounty());
                            warning.setWarningContent(site.getStationCounty() + item.getStationName() + "监测站湿度达到" + bigDecimal + "%");
                            warning.setWarningState(1);
                            warning.setElementValue(bigDecimal.toString());
                            warning.setElementType("hum");
                            warningService.insertWarningData(warning);
                        }
                    }
                });
                break;
            case "tem":
                list.stream().forEach(item -> {
                    if (number.equals(item.getStation())) {

                        QueryWrapper<TemScale> queryWrapper = new QueryWrapper<>();
                        queryWrapper.ge("hour", 0);
                        queryWrapper.le("hour", 24);
                        List<TemScale> temScales = temScaleService.list(queryWrapper);
                        // 获取实况气温>=用户设定温度的数据
                        List<TemScale> scales = temScales.stream().filter(s -> bigDecimal.compareTo(new BigDecimal(s.getThreshold())) > -1).collect(Collectors.toList());

                        if (scales.size() > 0) {
                            TemScale temScale = scales.stream().collect(Collectors.collectingAndThen(
                                    Collectors.reducing((a, b) -> Integer.parseInt(a.getThreshold()) > Integer.parseInt(b.getThreshold()) ? a : b), Optional::get));
                            Warning warning = new Warning();
                            warning.setCreateTime(presentDate);
                            warning.setStationName(item.getStationName());
                            warning.setStationNumber(item.getStation());
                            warning.setStationSite(item.getAddress());
                            WeatherStation site = weatherStationService.getStationInfoByName(item.getStation());
                            warning.setLat(String.valueOf(site.getLatitude()));
                            warning.setLon(String.valueOf(site.getLongitude()));
                            warning.setStationCounty(site.getStationCounty());
                            warning.setWarningState(1);
                            warning.setColor(temScale.getColor());
                            warning.setWarningContent("高温预警:" + site.getStationCounty() + item.getStationName() + "24小时内最高气温将升至" + bigDecimal + "℃以上");
                            warning.setElementValue(bigDecimal.toString());
                            warning.setElementType("tem");
                            warningService.insertWarningData(warning);
                        }
                    }
                });
                break;
            case "rain":
                list.stream().forEach(item -> {
                    if (number.equals(item.getStation())) {
                        if (bigDecimal.compareTo(new BigDecimal(item.getPrecipitation())) == 1) {
                            Warning warning = new Warning();
                            warning.setCreateTime(presentDate);
                            warning.setStationName(item.getStationName());
                            warning.setStationNumber(item.getStation());
                            warning.setStationSite(item.getAddress());
                            WeatherStation site = weatherStationService.getStationInfoByName(item.getStation());
                            warning.setLat(String.valueOf(site.getLatitude()));
                            warning.setLon(String.valueOf(site.getLongitude()));
                            warning.setStationCounty(site.getStationCounty());
                            warning.setWarningContent(site.getStationCounty() + item.getStationName() + "监测站降水达到" + bigDecimal + "mm");
                            warning.setWarningState(1);
                            warning.setElementType("rain");
                            warning.setElementValue(bigDecimal.toString());
                            warningService.insertWarningData(warning);
                        }

                    }
                });
                break;
            case "wind":
                list.stream().forEach(item -> {
                    if (number.equals(item.getStation())) {
                        if (bigDecimal.compareTo(new BigDecimal(item.getWind())) == 1) {
                            Warning warning = new Warning();
                            warning.setCreateTime(presentDate);
                            warning.setStationName(item.getStationName());
                            warning.setStationNumber(item.getStation());
                            warning.setWarningState(1);
                            warning.setStationSite(item.getAddress());
                            //风力等级
                            WindScale windScale = windScaleService.conversion(bigDecimal);
                            warning.setElementValue(String.valueOf(windScale.getWindScale()));
                            WeatherStation site = weatherStationService.getStationInfoByName(item.getStation());
                            warning.setLat(String.valueOf(site.getLatitude()));
                            warning.setLon(String.valueOf(site.getLongitude()));
                            warning.setStationCounty(site.getStationCounty());

                            //风力预警  等级管理
                            List<WarningWind> warningWinds = warningWindService.getListByType(1);


                            if (windScale.getWindScale()>=13){
                                windScale.setWindName("飓风以上");
                            }

                            warningWinds.stream().forEach(warningWind ->{
                                if (windScale.getWindScale() >= Integer.parseInt(warningWind.getThreshold())){
                                  warning.setColor(warningWind.getColor());
                                }
                            });

                            warning.setWarningContent(site.getStationCounty() + item.getStationName() + "监测站风速达到" + bigDecimal + "m/s;属于" + windScale.getWindScale() + "级" + windScale.getWindName());
                            warning.setElementValue(String.valueOf(windScale.getWindScale()));
                            warning.setElementType("wind");
                            warningService.insertWarningData(warning);
                        }
                    }
                });
                break;
            case "press":
                list.stream().forEach(item -> {
                    if (number.equals(item.getStation())) {
                        if (bigDecimal.compareTo(new BigDecimal(item.getPressure())) == 1) {
                            Warning warning = new Warning();
                            warning.setCreateTime(presentDate);
                            warning.setStationName(item.getStationName());
                            warning.setStationNumber(item.getStation());
                            WeatherStation site = weatherStationService.getStationInfoByName(item.getStation());
                            warning.setLat(String.valueOf(site.getLatitude()));
                            warning.setLon(String.valueOf(site.getLongitude()));
                            warning.setStationCounty(site.getStationCounty());
                            warning.setWarningContent(site.getStationCounty() + item.getStationName() + "监测站气压达到" + bigDecimal + "hpa");
                            warning.setWarningState(1);
                            warning.setStationSite(item.getAddress());
                            warning.setElementType("press");
                            warning.setElementValue(bigDecimal.toString());
                            warningService.insertWarningData(warning);
                        }

                    }
                });
                break;
        }
    }

}
